from datetime import datetime

from django.db import models
from django.contrib.auth.models import AbstractUser
from django.core.exceptions import ValidationError
# Create your models here.


class User(AbstractUser):
    nick_name = models.CharField(max_length=50, unique=True,
                                 verbose_name="用户名称", default="")
    birthday = models.DateField(verbose_name="生日", null=True)
    gender_tuple = (("male", "男"), ("female", "女"))
    gender = models.CharField(max_length=10, choices=gender_tuple)
    mobile = models.CharField(max_length=11, null=True, blank=True)  # 这个之后需要进行修改，电话号码不能为空

    class Meta:
        verbose_name = "用户"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.username


class EmailVerifyRecord(models.Model):  # 异步发送邮件的实现
    code = models.CharField(max_length=20, verbose_name="验证码")
    email = models.EmailField(max_length=50, verbose_name="邮箱")
    send_type_tuple = (("register", "注册"), ("forget", "找回密码"))
    send_type = models.CharField(max_length=10, choices=send_type_tuple)
    # 这里有一个细节问题，不加()就是不进行实例化进行调用，这样时间能够更加贴近实际
    send_time = models.DateTimeField(default=datetime.now)

    class Meta:
        verbose_name = "邮箱验证"
        verbose_name_plural = verbose_name

    def __str__(self):
        return "{0}({1})".format(self.code, self.email)

# def valid_mobile(mobile):
#     if len(mobile) != 11 or not mobile.isdigit():  # 最好别写成mobile.isdigit() == False这种形式
#         raise ValidationError(
#             ('%(mobile)s is not an true number of mobile'),
#             params={'mobile': mobile}
#         )
# class Util:
#
#     @staticmethod
#     def valid_mobile(mobile):
#         if len(mobile) != 11 or not mobile.isdigit():  # 最好别写成mobile.isdigit() == False这种形式
#             raise ValidationError(
#                 ('%(mobile)s is not an true number of mobile'),
#                 params={'mobile': mobile}
#             )


# class User(models.Model):
#
#     gender = (
#         ('male', "男"),
#         ('female', "女"),
#     )
#
#     name = models.CharField(max_length=128, unique=True)
#     password = models.CharField(max_length=256)
#     email = models.EmailField(unique=True)
#     sex = models.CharField(max_length=32, choices=gender, default="男")
#     c_time = models.DateTimeField(auto_now_add=True)
#     has_confirmed = models.BooleanField(default=False)
#
#     def __str__(self):
#         return self.name
#
#     class Meta:
#         ordering = ["-c_time"]
#         verbose_name = "用户"
#         verbose_name_plural = "用户"


class ConfirmString(models.Model):
    code = models.CharField(max_length=256)
    user = models.OneToOneField('User')
    c_time = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.user.name + ":   " + self.code

    class Meta:

        ordering = ["-c_time"]
        verbose_name = "确认码"
        verbose_name_plural = "确认码"
